package com.text.mqtt_text.controller;


import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.text.mqtt_text.mqtt.ChatMessage;
import com.text.mqtt_text.mqtt.ConfigScraper;
import com.text.mqtt_text.pojo.Memory;
import com.text.mqtt_text.pojo.StudyDTO;
import com.text.mqtt_text.service.StudyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

import static com.text.mqtt_text.mqtt.Main.thinks;

@Slf4j
@RestController
public class StudyController {

    @Autowired
    StudyService studyService;


    /**
     * 数据库获取小智对话信息
     * @return List<StudyDTO>
     */
    @CrossOrigin
    @GetMapping("/study")
    public List<StudyDTO> study(){
        List<StudyDTO> list;
        list = studyService.selectChatHistory();
        return list;
    }

    /**
     * 官网小智对话信息
     * @return List<StudyDTO>
     */
//    @CrossOrigin
//    @GetMapping("/study")
//    public String study() throws IOException {
//        ChatMessage.Message();
//        Path path = Paths.get("E:\\JAVA\\javaproject\\study_mqtt_text\\downloaded_configs\\chatmassage.txt");
//        String data = Files.readString(path);
//        log.info(data);
//        return data;
//    }

    Memory oldMemory = new Memory();
    //小智记忆体
    @CrossOrigin
    @GetMapping("/status")
    public Memory status() throws IOException {
       Memory memory= new Memory();
        memory = studyService.selectChatMemory();//使用本地的小智

        //使用官网的小智
//        ConfigScraper.config();
//        Path path = Paths.get("E:\\JAVA\\javaproject\\study_mqtt_text\\downloaded_configs\\memory.txt");
//        String data = Files.readString(path);
//        memory.setSummaryMemory(data);

        //判断数据有没有更新
        if(Objects.equals(memory.getSummaryMemory(), oldMemory.getSummaryMemory())){//如果相同
            log.info("使用之前的数据");
            return oldMemory;
        }

        String thinks  = thinks(memory);

        // 删除开头的 ```json 和结尾的 ```
        String cleanJson = thinks
                .replaceAll("^```json", "")  // 删除开头的 ```json
                .replaceAll("```$", "");     // 删除结尾的 ```


//        log.info(cleanJson);
        //处理json数据
        JsonObject jsonObject = JsonParser.parseString(cleanJson).getAsJsonObject();

        String analysis = jsonObject.get("analysis").getAsString();
        String suggestion = jsonObject.get("suggestion").getAsString();
        String variation = jsonObject.get("variation").getAsString();

        log.info("analysis:{}", analysis);
        log.info("suggestion:{}", suggestion);
        log.info("variation:{}", variation);
        //封装
        memory.setAnalysis(analysis);
        memory.setSuggestion(suggestion);
        memory.setVariation(variation);

        oldMemory.setSummaryMemory(memory.getSummaryMemory());
        oldMemory.setUpdatedAt(memory.getUpdatedAt());
        oldMemory.setAnalysis(analysis);
        oldMemory.setSuggestion(suggestion);
        oldMemory.setVariation(variation);

        return oldMemory;
    }

}
